<template>
  <div class="register-container">
    <div class="register-content">
      <div class="register-card">
        <div class="register-header">
          <h2>用户注册</h2>
          <p class="subtitle">欢迎加入博物馆导览系统</p>
        </div>
        <el-form 
          :model="registerForm" 
          :rules="registerRules" 
          ref="registerFormRef" 
          class="register-form"
        >
          <el-form-item prop="username">
            <el-input
              v-model="registerForm.username"
              :prefix-icon="User"
              placeholder="请输入用户名"
              class="material-input"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="registerForm.password"
              :prefix-icon="Lock"
              type="password"
              placeholder="请输入密码"
              show-password
              class="material-input"
            />
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input
              v-model="registerForm.confirmPassword"
              :prefix-icon="Lock"
              type="password"
              placeholder="请确认密码"
              show-password
              class="material-input"
            />
          </el-form-item>
          <el-form-item prop="role">
            <el-select
              v-model="registerForm.role"
              placeholder="请选择角色"
              class="material-select"
            >
              <el-option label="游客" value="VISITOR" />
            </el-select>
          </el-form-item>
          <div class="optional-fields">
            <el-form-item prop="realName">
              <el-input
                v-model="registerForm.realName"
                :prefix-icon="User"
                placeholder="请输入真实姓名（选填）"
                class="material-input"
              />
            </el-form-item>
            <el-form-item prop="phone">
              <el-input
                v-model="registerForm.phone"
                :prefix-icon="Phone"
                placeholder="请输入手机号（选填）"
                class="material-input"
              />
            </el-form-item>
            <el-form-item prop="email">
              <el-input
                v-model="registerForm.email"
                :prefix-icon="Message"
                placeholder="请输入邮箱（选填）"
                class="material-input"
              />
            </el-form-item>
          </div>
          <el-form-item>
            <el-button
              type="primary"
              :loading="loading"
              @click="handleRegister"
              class="register-button"
              :class="{ 'button-loading': loading }"
            >
              {{ loading ? '注册中...' : '注册' }}
            </el-button>
          </el-form-item>
        </el-form>
        <div class="register-options">
          <el-button 
            text 
            @click="goToLogin" 
            class="login-link"
          >
            已有账号？去登录
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, Message, Phone } from '@element-plus/icons-vue'
import { register } from '@/api/user'  // 导入注册接口

const router = useRouter()
const registerFormRef = ref(null)
const loading = ref(false)

const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  role: 'VISITOR',
  realName: '',
  phone: '',
  email: ''
})

const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    if (registerForm.confirmPassword !== '') {
      registerFormRef.value?.validateField('confirmPassword')
    }
    callback()
  }
}

const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== registerForm.password) {
    callback(new Error('两次输入密码不一致!'))
  } else {
    callback()
  }
}

const validatePhone = (rule, value, callback) => {
  if (value && !/^1[3-9]\d{9}$/.test(value)) {
    callback(new Error('请输入正确的手机号'))
  } else {
    callback()
  }
}

const validateEmail = (rule, value, callback) => {
  if (value && !/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(value)) {
    callback(new Error('请输入正确的邮箱'))
  } else {
    callback()
  }
}

const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, validator: validatePass, trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, validator: validatePass2, trigger: 'blur' }
  ],
  role: [
    { required: true, message: '请选择角色', trigger: 'change' }
  ],
  phone: [
    { validator: validatePhone, trigger: 'blur' }
  ],
  email: [
    { validator: validateEmail, trigger: 'blur' }
  ]
}

const handleRegister = async () => {
  if (!registerFormRef.value) return
  
  await registerFormRef.value.validate(async (valid) => {
    if (!valid) return
    
    loading.value = true
    try {
      // 调用注册接口，添加 confirmPassword 字段
      await register({
        username: registerForm.username,
        password: registerForm.password,
        confirmPassword: registerForm.confirmPassword,  // 添加确认密码
        role: registerForm.role,
        realName: registerForm.realName || undefined,
        phone: registerForm.phone || undefined,
        email: registerForm.email || undefined
      })
      
      ElMessage.success('注册成功，请登录')
      router.push('/login')
    } catch (error) {
      ElMessage.error(error.response?.data?.message || '注册失败')
    } finally {
      loading.value = false
    }
  })
}

const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  height: 100vh;
  background: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url('@/assets/images/museum.png');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  overflow: hidden;
}

.register-content {
  width: 100%;
  max-width: 480px;
  padding: 20px;
  perspective: 1000px;
  max-height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.register-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  padding: 40px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2), 0 0 20px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  transform-style: preserve-3d;
  transition: all 0.3s ease;
  max-height: calc(100vh - 120px);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  margin: 60px 0;
}

.register-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 50px rgba(0, 0, 0, 0.25), 0 0 30px rgba(0, 0, 0, 0.15);
}

.register-header {
  text-align: center;
  margin-bottom: 30px;
  flex-shrink: 0;
}

.register-header h2 {
  font-size: 28px;
  color: #333;
  margin-bottom: 8px;
  font-weight: 600;
}

.subtitle {
  color: #666;
  font-size: 16px;
  margin: 0;
}

.register-form {
  margin-bottom: 20px;
  overflow-y: auto;
  flex: 1;
  padding-right: 0;
  padding-top: 10px;
  padding-bottom: 10px;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.register-form::-webkit-scrollbar {
  display: none;
}

.material-input :deep(.el-input__wrapper) {
  background: transparent;
  border: none;
  border-radius: 0;
  border-bottom: 2px solid #dcdfe6;
  box-shadow: none !important;
  transition: all 0.3s;
}

.material-input :deep(.el-input__wrapper):hover,
.material-input :deep(.el-input__wrapper.is-focus) {
  border-bottom-color: #409EFF;
}

.material-input :deep(.el-input__inner) {
  height: 48px;
  line-height: 48px;
  padding-left: 0;
}

.material-select {
  width: 100%;
}

.material-select :deep(.el-input__wrapper) {
  background: transparent;
  border: none;
  border-radius: 0;
  border-bottom: 2px solid #dcdfe6;
  box-shadow: none !important;
}

.register-button {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 500;
  margin-top: 24px;
  background: linear-gradient(45deg, #409EFF, #36cfc9);
  border: none;
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease;
}

.register-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(
    120deg,
    transparent,
    rgba(255, 255, 255, 0.2),
    transparent
  );
  transition: all 0.5s;
}

.register-button:hover::before {
  left: 100%;
}

.register-button.button-loading {
  background: #409EFF;
}

.register-options {
  flex-shrink: 0;
  padding-top: 10px;
  padding-bottom: 10px;
  text-align: center;
}

.login-link {
  color: #409EFF;
  font-size: 14px;
  transition: all 0.3s ease;
  margin-bottom: 20px;
}

.login-link:hover {
  color: #66b1ff;
  transform: translateY(-1px);
}

.optional-fields {
  padding-top: 16px;
  margin-top: 16px;
  border-top: 1px dashed #dcdfe6;
}

@media (max-width: 768px) {
  .register-content {
    padding: 16px;
  }
  
  .register-card {
    padding: 24px;
    margin: 40px 0; /* 在移动端适当减小边距 */
  }
  
  .register-header h2 {
    font-size: 24px;
  }
  
  .subtitle {
    font-size: 14px;
  }
}
</style> 